home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / freeWAIS-sf-1.1 / ir / byte_order.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-13  |  3.8 KB  |  128 lines

  1.  
  2. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  3.  
  4. /* byte_order:
  5.  *  This file contains some useful routines to handle the differences between
  6.  *  big and little endian machines. The macros may look a little unwieldy, but
  7.  *  any modern optimiser will take care of things quite nicely. 
  8.  *
  9.  * $Log: byte_order.h,v $
  10.  * Revision 1.4  1994/12/13  17:03:58  pfeifer
  11.  * *** empty log message ***
  12.  *
  13.  * Revision 1.3  1994/05/02  13:08:22  pfeifer
  14.  * renamed BIG_ENDIAN to BIGENDIAN
  15.  *
  16.  * Revision 1.2  1994/03/08  20:56:54  pfeifer
  17.  * Patchlevel 04
  18.  *
  19.  * Revision 1.1  1993/02/16  15:05:35  freewais
  20.  * Initial revision
  21.  *
  22.  * Revision 1.1  92/03/15  08:49:03  jonathan
  23.  * Initial revision
  24.  * 
  25.  */
  26.  
  27. #include "../config.h"
  28.  
  29.  
  30. /* ASSIGN_HELPER:
  31.     This macro turns a pointer to long into a pointer to a {1,2,3} byte value
  32. */
  33.  
  34. #ifdef BIGENDIAN
  35. #define ASSIGN_HELPER(var,size) (FOUR_BYTE*)((ONE_BYTE*) &var+(sizeof(FOUR_BYTE)-size))
  36. #else
  37. #define ASSIGN_HELPER(var,size)  (FOUR_BYTE *)&var
  38. #endif 
  39.  
  40. /*
  41.   ASSIGN(var,size,src,unit,offset): 
  42.      var : variable to store into
  43.      size: how big is value?
  44.      src : source address
  45.      unit,offset: 
  46.       These paramaters are used to help optimise the macro when used with 
  47.       structured data. unit should be the size of one record, and offset should
  48.       be the offset of this item within that record. These paramaters are *not*
  49.       used to calculate the source address - they are used purely to let the
  50.       compiler take advantage of possibly aligned data. 
  51.  
  52.       ASSIGN_NATIVE uses native byte ordering
  53.       ASSIGN_CANON  uses canonical (big_endian) byte ordering
  54. */
  55.  
  56. #if defined(NATIVE_ORDER) || defined(BIGENDIAN)
  57. #define ASSIGN ASSIGN_NATIVE
  58. #else
  59. #define ASSIGN ASSIGN_CANON
  60. #endif
  61.  
  62. #define ASSIGN_NATIVE(var,size,src,unit,offset) {\
  63.   FOUR_BYTE *dst;\
  64.     dst = ASSIGN_HELPER(var,size);\
  65.   switch(size) \
  66.     {\
  67.     case 1:\
  68.       var = 0;\
  69.       *(unsigned ONE_BYTE*)dst= *(unsigned ONE_BYTE*)(src);\
  70.       break;\
  71.     case 2:\
  72.       if (!(unit % TWO_BYTE_ALIGN) && !(offset % TWO_BYTE_ALIGN)) {\
  73.         var = 0;\
  74.     *(unsigned TWO_BYTE *)dst = *(unsigned TWO_BYTE *)(src);\
  75.       } else {\
  76.     var =0;\
  77.     *(unsigned ONE_BYTE *)dst = *(unsigned ONE_BYTE *)(src);\
  78.     *((unsigned ONE_BYTE *)dst+1) = *(unsigned ONE_BYTE*)(src+1);\
  79.       }\
  80.       break;\
  81.     case 3:\
  82.       var = 0;\
  83.       *(unsigned ONE_BYTE *)dst = *(unsigned ONE_BYTE *)(src);\
  84.       *((unsigned ONE_BYTE *)dst+1) = *(unsigned ONE_BYTE*)(src+1);\
  85.       *((unsigned ONE_BYTE *)dst+2) = *(unsigned ONE_BYTE*)(src+2);\
  86.       break;\
  87.     case 4:\
  88.       if (!(unit % FOUR_BYTE_ALIGN) && !(offset % FOUR_BYTE_ALIGN)) { \
  89.     *dst = *(unsigned FOUR_BYTE *)(src);\
  90.       } else {\
  91.     *(unsigned ONE_BYTE *)dst = *(unsigned ONE_BYTE *)(src);\
  92.     *((unsigned ONE_BYTE *)dst+1) = *(unsigned ONE_BYTE*)(src+1);\
  93.     *((unsigned ONE_BYTE *)dst+2) = *(unsigned ONE_BYTE*)(src+2);\
  94.     *((unsigned ONE_BYTE *)dst+3) = *(unsigned ONE_BYTE*)(src+3);\
  95.       }\
  96.       break;\
  97.     }\
  98. }
  99.  
  100. #define ASSIGN_CANON(var,size,src,unit,offset) {\
  101.   FOUR_BYTE *dst;\
  102.     dst = ASSIGN_HELPER(var,size);\
  103.   switch(size) \
  104.     {\
  105.     case 1:\
  106.       var = 0;\
  107.       *(unsigned ONE_BYTE*)dst= *(unsigned ONE_BYTE*)(src);\
  108.       break;\
  109.     case 2:\
  110.       var =0;\
  111.       *(unsigned ONE_BYTE *)dst = *(unsigned ONE_BYTE *)(src+1);\
  112.       *((unsigned ONE_BYTE *)dst+1) = *(unsigned ONE_BYTE*)(src);\
  113.       break;\
  114.     case 3:\
  115.       var = 0;\
  116.       *(unsigned ONE_BYTE *)dst = *(unsigned ONE_BYTE *)(src+2);\
  117.       *((unsigned ONE_BYTE *)dst+1) = *(unsigned ONE_BYTE*)(src+1);\
  118.       *((unsigned ONE_BYTE *)dst+2) = *(unsigned ONE_BYTE*)(src);\
  119.       break;\
  120.     case 4:\
  121.       *(unsigned ONE_BYTE *)dst = *(unsigned ONE_BYTE *)(src+3);\
  122.       *((unsigned ONE_BYTE *)dst+1) = *(unsigned ONE_BYTE*)(src+2);\
  123.       *((unsigned ONE_BYTE *)dst+2) = *(unsigned ONE_BYTE*)(src+1);\
  124.       *((unsigned ONE_BYTE *)dst+3) = *(unsigned ONE_BYTE*)(src);\
  125.       break;\
  126.     }\
  127. }
  128.